﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;


namespace MassCoverDownloader
{
    public partial class MainForm : Form
    {

        private CoreDownloader cd;
        private int subDirCount;
        private string currRes;

        public MainForm()
        {
            InitializeComponent();
        }

        private void browseButton_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.SelectedPath = System.IO.Directory.GetCurrentDirectory();
            dialog.ShowDialog();
            selectedPathTextBox.Text = dialog.SelectedPath;
            cd = new CoreDownloader(dialog.SelectedPath, overrideCheckBox.Checked);
            subDirCount = cd.getCount();
            numberToProcessLabel.Text = subDirCount.ToString();
            progressBar.Maximum = subDirCount;
            progressBar.Value = 0;
            progressBar.Step = 1;
        }

        private void StartStopButton_Click(object sender, EventArgs e)
        {
            StartStopButton.Enabled = false;
            browseButton.Enabled = false;
            resultLogTextBox.Clear();

            if (backgroundWorker.IsBusy != true)
            {
                // Start the asynchronous operation.
                backgroundWorker.RunWorkerAsync();
            }
            //StartStopButton.Enabled = true;
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            if (backgroundWorker.WorkerSupportsCancellation == true)
            {
                // Cancel the asynchronous operation.
                backgroundWorker.CancelAsync();
            }
        }

        // This event handler is where the time-consuming work is done.
        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            for (int i = 1; i <= subDirCount; i++)
            {
                if (worker.CancellationPending == true)
                {
                    e.Cancel = true;
                    StartStopButton.Enabled = true;
                    browseButton.Enabled = true;
                    break;
                }
                else
                {
                    // Perform a time consuming operation and report progress.
                    System.Threading.Thread.Sleep(50);
                    currRes = cd.next();
                    worker.ReportProgress((int)((double)i / (double)subDirCount) * 100);
                }
            }
        }

        // This event handler updates the progress.
        private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            resultLogTextBox.AppendText(currRes + "\r\n");

            try
            {
                CoverPictureBox.Image = Image.FromFile(currRes);
            }
            catch (Exception)
            {
                Bitmap flag = new Bitmap(200, 100);
                Graphics flagGraphics = Graphics.FromImage(flag);
                int red = 0;
                int white = 11;
                while (white <= 100)
                {
                    flagGraphics.FillRectangle(Brushes.Red, 0, red, 200, 10);
                    flagGraphics.FillRectangle(Brushes.White, 0, white, 200, 10);
                    red += 20;
                    white += 20;
                }
                CoverPictureBox.Image = flag;
            }

            currCountLabel.Text = (progressBar.Value + 1).ToString();
            progressBar.Value += 1;
        }

        // This event handler deals with the results of the background operation.
        private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled == true)
            {
                resultLogTextBox.Text = "Canceled!";
            }
            else if (e.Error != null)
            {
                resultLogTextBox.Text = "Error: " + e.Error.Message;
            }
            else
            {
                resultLogTextBox.AppendText("\r\n...Done!");
                System.Windows.Forms.MessageBox.Show(cd.generateReport(), "Finish raport");
                cd.closeLog();
                StartStopButton.Enabled = true;
                browseButton.Enabled = true;
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }

    }
}
